//
//  AddDvViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/7/9.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "AddDvViewController.h"
#import "IGTwoCodeVC.h"
#import "IGiGalleryBindApi.h"
#import "AppDelegate.h"

@interface AddDvViewController () <UITableViewDelegate, UITableViewDataSource> {
    NSString *title;
}

@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *table;

@property(weak,nonatomic)IBOutlet UITableViewCell *cell;

@property(weak,nonatomic)IBOutlet UITextField *textView;
@property(weak,nonatomic)IBOutlet UILabel *msgLabel;
@property(weak,nonatomic)IBOutlet UIButton *btn;
    
@property (nonatomic, strong) IGiGalleryBindApi *bindApi;

@end

@implementation AddDvViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    self.title = @"添加画屏";
    self.title = NSLocalizedString(@"Add iGallery", nil);
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, GSCALE(435))];
    header.backgroundColor=[UIColor whiteColor];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 35, GSCALE(197), GSCALE(303))];
    img.image = [UIImage imageNamed:@"igallery"];
    img.centerX=header.centerX;
    img.contentMode = UIViewContentModeScaleAspectFit;
    [header addSubview:img];
    
    UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(0, img.bottom+25, KSCREENWIDTH, 23)];

    msg.font = [UIFont font16];
    msg.textAlignment = NSTextAlignmentCenter;
    msg.numberOfLines = 0;
//    msg.text = [NSString stringWithFormat:@"设备ID: %@", self.mac_id];
    NSString *deviceID = NSLocalizedString(@"Device ID", nil);
    msg.text = [NSString stringWithFormat:@"%@: %@", deviceID,self.mac_id];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:msg.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8;// 字体的行间距
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.length)];
    msg.attributedText = str;
    [header addSubview:msg];
    
    self.table.tableHeaderView = header;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT-header.height-50-64)];
    view.backgroundColor=[UIColor whiteColor];
    UIButton *add = [UIButton buttonWithType:UIButtonTypeCustom];
    add.backgroundColor = [UIColor appColor];
    add.layer.cornerRadius = 20.f;
    add.layer.masksToBounds = YES;
    add.frame = CGRectMake(0, 0, GSCALE(300), GSCALE(44));
    add.titleLabel.font = [UIFont font16];
    [add setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [add setTitle:@"添加" forState:UIControlStateNormal];
    [add setTitle:NSLocalizedString(@"Add", nil) forState:UIControlStateNormal];
    [add addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    add.center = CGPointMake(view.width/2, self.textView.bottom+40);
    
    [view addSubview:add];
    
    self.table.tableFooterView = view;
    self.table.backgroundColor = [UIColor c2Color];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    self.cell=cell;
    CGFloat x = cell.separatorInset.left;
    cell.textLabel.font = [UIFont font18];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UITextField *textView = [[UITextField alloc] initWithFrame:CGRectMake(x, 0, KSCREENWIDTH-2*x, 50)];
    self.textView=textView;
    textView.borderStyle = UITextBorderStyleNone;
    textView.leftViewMode = UITextFieldViewModeAlways;
    textView.rightViewMode=UITextFieldViewModeUnlessEditing;
    textView.font = [UIFont font18];
    textView.clearButtonMode = UITextFieldViewModeWhileEditing;
    textView.textColor=[UIColor c4Color];
    [textView addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [textView addTarget:self action:@selector(textBeainEditing:) forControlEvents:UIControlEventEditingDidBegin];
    [textView addTarget:self action:@selector(textEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
    textView.tag = indexPath.row;
    if (indexPath.row==0) {
        textView.keyboardType = UIKeyboardTypeDefault;
        UILabel *left = [[UILabel alloc] initWithFrame:CGRectZero];
//        left.text = @"设备名称";
        left.text = NSLocalizedString(@"Device Name", nil);
        left.textColor=[UIColor c5Color];
        left.font = [UIFont font18];
        [left sizeToFit];
        left.width += 10;
        textView.leftView = left;
    
        UILabel *right = [[UILabel alloc] initWithFrame:CGRectZero];
//        right.text = NSLocalizedString(@"My iGallery");
        right.text =[NSString stringWithFormat:@"%@-%@", NSLocalizedString(@"My iGallery",nil),self.mac_id];
        title=right.text;
        right.textColor=[UIColor c4Color];
        right.textAlignment=NSTextAlignmentLeft;
        right.font = [UIFont font16];
        [right sizeToFit];
        right.width=textView.width-left.width-20;
        right.left=left.right;
        textView.rightView=right;
        [cell.contentView addSubview:textView];
        
        UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(15,cell.bottom, KSCREENWIDTH, 1)];
        line.backgroundColor = [UIColor c6Color];
        [cell.contentView addSubview:line];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)addAction:(UIButton *)btn {
    [self.view endEditing:YES];
    if (title.length == 0) {

//        title=@"我的画屏";
//        title=NSLocalizedString(@"My iGallery");

        title=[NSString stringWithFormat:@"%@-%@",NSLocalizedString(@"My iGallery", nil), self.mac_id];
    }
    
    self.bindApi = [[IGiGalleryBindApi alloc] initWithMacId:self.mac_id title:title type:IGiGalleryTypeiGallery];
    
    __weak typeof(self) weakSelf = self;
    [self.bindApi startWithDecodeClass:[IGiGalleryBindResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
//            [weakSelf presentMessageTips:@"绑定成功"];
            [weakSelf presentMessageTips:NSLocalizedString(@"Linked Successfully", nil)];
            if (![kAPPDELEGATE rootTabBarVC]) {
                [Tool performBlock:^{
                    [kAPPDELEGATE configTabBarVC];
                } afterDelay:1];
            } else {
                [UIViewController ig_delectViewController:[IGTwoCodeVC class]];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        }
    }];
}

- (void)textBeainEditing:(UITextField *)textView {
    self.table.bounces = YES;
    textView.rightViewMode=UITextFieldViewModeNever;
}

- (void)textEndEditing:(UITextField *)textView {
    self.table.bounces = NO;
    if(textView.text.length!=0){
        textView.rightViewMode=UITextFieldViewModeNever;
    }else{
        textView.rightViewMode=UITextFieldViewModeUnlessEditing;
    }
}

- (void)textChange:(UITextField *)textView {
    if (textView.text.length > 12) {
        textView.text = [textView.text substringToIndex:12];
    }
    title = textView.text;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
